home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1996 #1 / Amiga Plus CD - 1996 - No. 1.iso / pd / netz / xbtx_v1.1 / iofile.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-26  |  3.4 KB  |  147 lines

  1. /*
  2. **    $Id: IOFile.cpp 1.4 1995/09/26 19:45:11 olsen Exp olsen $
  3. **
  4. **    :ts=4
  5. */
  6.  
  7. /*
  8.  * Copyright © 1995 by Olaf Barthel, All Rights Reserved
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions
  12.  * are met:
  13.  * 1. Redistributions of source code must retain the above copyright
  14.  *    notice, this list of conditions and the following disclaimer.
  15.  * 2. Redistributions in binary form must reproduce the above copyright
  16.  *    notice, this list of conditions and the following disclaimer in the
  17.  *    documentation and/or other materials provided with the distribution.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
  20.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
  22.  * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  25.  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26.  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27.  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  28.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  *
  30.  * This software has not been validated by the ``Bundesamt fuer Zulassungen in
  31.  * der Telekommunikation'' of the ``Deutsche Bundepost Telekom'' and thus
  32.  * must not be used for accessing the BTX-Network of the Telekom in Germany.
  33.  *
  34.  * Diese Software hat keine Zulassung durch das Bundesamt fuer Zulassungen in
  35.  * der Telekommunikation der Deutschen Bundespost Telekom und darf daher nicht
  36.  * am Netz der Deutschen Bundespost Telekom in Deutschland betrieben werden.
  37.  */
  38.  
  39. #include "IOFile.hpp"
  40.  
  41. #include <exec/memory.h>
  42. #include <dos/dosextens.h>
  43. #include <dos/stdio.h>
  44.  
  45. #include <clib/exec_protos.h>
  46. #include <clib/dos_protos.h>
  47.  
  48. #ifdef __SASC
  49. #include <pragmas/exec_pragmas.h>
  50. #include <pragmas/dos_pragmas.h>
  51.  
  52. extern struct ExecBase *SysBase;
  53. extern struct DosLibrary *DOSBase;
  54. #endif    // __SASC
  55.  
  56. /* this macro lets us long-align structures on the stack */
  57. #define D_S(type,name) char a_##name[sizeof(type)+3]; \
  58.                        type *name = (type *)((LONG)(a_##name+3) & ~3);
  59.  
  60. IOFile::IOFile()
  61. {
  62.     FileHandle = NULL;
  63. }
  64.  
  65. IOFile::~IOFile()
  66. {
  67.     Close();
  68. }
  69.  
  70. LONG IOFile::Open(CONST STRPTR Channel,ULONG Unit,ULONG Baud,BOOL RTS_CTS)
  71. {
  72.     LONG Error;
  73.  
  74.     Close();
  75.  
  76.     if(FileHandle = ::Open((char *)Channel,MODE_OLDFILE))
  77.     {
  78.         BPTR FileLock = Lock((char *)Channel,SHARED_LOCK);
  79.  
  80.         if(FileLock)
  81.         {
  82. //            struct FileInfoBlock __aligned FileInfo;
  83.             D_S(struct FileInfoBlock,FileInfo);
  84.  
  85.             if(Examine(FileLock,FileInfo))
  86.             {
  87.                 Size = FileInfo->fib_Size;
  88.                 BytesRead = 0;
  89.  
  90.                 UnLock(FileLock);
  91.  
  92.                 SetVBuf(FileHandle,NULL,BUF_FULL,4096);
  93.  
  94.                 return(0);
  95.             }
  96.             else
  97.                 Error = IoErr();
  98.  
  99.             UnLock(FileLock);
  100.         }
  101.         else
  102.             Error = IoErr();
  103.  
  104.         Close();
  105.     }
  106.     else
  107.         Error = IoErr();
  108.  
  109.     return(Error);
  110. }
  111.  
  112. VOID IOFile::Close(VOID)
  113. {
  114.    if(FileHandle)
  115.    {
  116.        ::Close(FileHandle);
  117.        FileHandle = NULL;
  118.    }
  119. }
  120.  
  121. LONG IOFile::GetChar(LONG Timeout)
  122. {
  123.     if(FileHandle)
  124.     {
  125.         LONG Result = FGetC(FileHandle);
  126.  
  127.         if(Result < 0)
  128.             return(CHANNELERR_EOF);
  129.         else
  130.         {
  131.             BytesRead++;
  132.  
  133.             return(Result);
  134.         }
  135.     }
  136.     else
  137.         return(CHANNELERR_EOF);
  138. }
  139.  
  140. LONG IOFile::Waiting(VOID)
  141. {
  142.     if(FileHandle)
  143.         return(Size - BytesRead);
  144.     else
  145.         return(0);
  146. }
  147.